package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.AppointmentVaccine;
import com.example.entity.Vaccine;
import com.example.entity.vo.appointmentVaccineVO;
import com.example.entity.vo.vaccineVO;
import com.example.service.IAppointmentVaccineService;
import com.example.service.IVaccineService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 可预约疫苗列表 前端控制器
 * </p>
 *
 * @author wyx
 * @since 2025-03-19
 */
@CrossOrigin//跨域访问
@Slf4j
@Api(tags = "预约疫苗管理模块")
@RestController
@RequestMapping("/vaccineappointment")
public class AppointmentVaccineController {
    @Autowired
    IAppointmentVaccineService appointmentVaccineService;
    @Autowired
    IVaccineService vaccineService;

    // 分页查询可预约疫苗列表
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name, String address) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        Page<AppointmentVaccine> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<AppointmentVaccine> queryWrapper = new LambdaQueryWrapper<>();

        // 根据地址进行模糊查询
        if (address != null) {
            queryWrapper.like(AppointmentVaccine::getAddress, address);
        }

        // 根据疫苗名称进行模糊查询，并根据疫苗ID进行关联查询
        if (name != null) {
            LambdaQueryWrapper<Vaccine> vaccineQueryWrapper = new LambdaQueryWrapper<>();
            vaccineQueryWrapper.like(Vaccine::getName, name);
            List<Vaccine> list = vaccineService.list(vaccineQueryWrapper);
            if (list != null && !list.isEmpty()) {
                // 获取疫苗ID列表
                List<Integer> vaccineIds = list.stream().map(Vaccine::getId).collect(Collectors.toList());
                // 根据疫苗ID在 AppointmentVaccine 中进行查找
                queryWrapper.in(AppointmentVaccine::getVaccineId, vaccineIds);
            }
        }

        // 查询疫苗数据
        Page<AppointmentVaccine> appointmentVaccinePage = appointmentVaccineService.page(pageInfo, queryWrapper);

        // 将 AppointmentVaccine 对象转换为 appointmentVaccineVO 对象，并填充 type 字段
        IPage<appointmentVaccineVO> appointmentVaccineVOIPage = appointmentVaccinePage.convert(appointmentVaccine -> {
            appointmentVaccineVO vo = new appointmentVaccineVO();
            BeanUtils.copyProperties(appointmentVaccine, vo);
            // 假设 type 字段需要根据某些条件进行填充
//            vo.setName(vaccineService.getById(appointmentVaccine.getVaccineId()).getName());
            // 获取疫苗名称并填充到 vo 对象中
            Vaccine vaccine = vaccineService.getById(appointmentVaccine.getVaccineId());
            if (vaccine != null) {
                vo.setName(vaccine.getName());
            } else {
                // 处理疫苗信息不存在的情况，避免空指针异常
                vo.setName("未知疫苗");
            }
            return vo;
        });

        return R.success(appointmentVaccineVOIPage);
    }

    // 新增预约疫苗
    @PostMapping("/add")
    public R add(AppointmentVaccine appointmentVaccine) {
        log.info("新增预约疫苗：{}", appointmentVaccine);
        appointmentVaccineService.save(appointmentVaccine);
        return R.success("新增预约疫苗成功");
    }

    // 修改预约疫苗
    @PutMapping("/update")
    public R update(AppointmentVaccine appointmentVaccine) {
        log.info("修改预约疫苗：{}", appointmentVaccine);
        appointmentVaccineService.updateById(appointmentVaccine);
        return R.success("修改预约疫苗成功");
    }

    // 删除预约疫苗
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        log.info("删除预约疫苗：{}", id);
        appointmentVaccineService.removeById(id);
        return R.success("删除预约疫苗成功");
    }



}
